package com.spzx.product.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.SkuQuery;
import com.spzx.product.service.ProductService;
import com.spzx.product.service.ProductSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name = "商品列表")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductSkuService productSkuService;

    @Operation(summary = "分页带条件查询")
    @GetMapping("/list")
    public TableDataInfo getPageList(Product product) {
        //开启分页
        startPage();
        //调用productSpecService中的方法
        List<Product> pageInfo = productService.getPageList(product);
        return getDataTable(pageInfo);
    }

    @Operation(summary = "添加商品")
    @PostMapping
    public AjaxResult saveProduct(@RequestBody Product product) {
        //调用自定义的保存商品的方法
        int flag = productService.saveProduct(product);
        return toAjax(flag);
    }

    @Operation(summary = "根据Id查询商品信息")
    @GetMapping("/{id}")
    public AjaxResult getProductById(@PathVariable Long id) {
        Product product = productService.getProductById(id);
        return AjaxResult.success(product);
    }

    @Operation(summary = "根据id更新商品信息")
    @PutMapping
    public AjaxResult updateProductById(@RequestBody Product product) {
        //调用自定义的方法
        productService.updateProductById(product);
        return AjaxResult.success();
    }

    @Operation(summary = "根据ID删除商品信息")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteProductById(@PathVariable List<Long> ids) {
        //调用自带的删除方法
        productService.deleteProductById(ids);
        return AjaxResult.success();
    }

    @Operation(summary = "商品审核")
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult updateAuditStatus(@PathVariable Long id, @PathVariable Integer auditStatus) {
        productService.updateAuditStatus(id, auditStatus);
        return AjaxResult.success();
    }

    @Operation(summary = "根据ID更新上下架")
    @GetMapping("/updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        //调用自定义的方法
        productService.updateStatus(id, status);
        return AjaxResult.success();
    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用获取畅销商品的接口")
    @GetMapping("/getTopProductSku")
    public R<List<ProductSku>> getTopProductSku() {
        //调用ProductService中获取畅销商品的方法
        List<ProductSku> productSkuList = productService.getTopProductSku();
        return R.ok(productSkuList);
    }

    @InnerAuth
    @Operation(summary = "商品列表搜索")
    @GetMapping("/list/{pageNum}/{pageSize}")
    public R<TableDataInfo> skuList(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            SkuQuery skuQuery
    ) {
        PageHelper.startPage(pageNum,pageSize);
        List<ProductSku> productSkuList = productService.selectProductSkuList(skuQuery);
        return R.ok(getDataTable(productSkuList));
    }



}
